import Login from 'ant-design-pro/lib/Login';
import { Alert, Checkbox } from 'antd';
import * as React from "react";
import { Link } from 'react-router-dom';
import './style.less';
import {FetchAPI} from "../../utils/fetch-middleware";
import {Modal} from "antd/lib/index";
import {setCurrentLoginUser} from '../../common/permission';
import logo from '../../img/mhlogo.jpg';
import Redirect from "react-router-dom/es/Redirect";
import * as ReactDOM from "react-dom";

const { Tab, UserName, Password, Mobile, Captcha, Submit } = Login;
class LoginDemo extends React.Component {
    state = {
        notice: '',
        autoLogin: true,
    }

    warning(message) {
        Modal.warning({
            title: '系统提示',
            content: message,
        });
    }

    error() {
        Modal.error({
            title: '系统警告',
            content: '系统内部错误，请联系管理员～',
        });
    }

    onSubmit = (err, values) => {
        // console.log('value collected ->', { ...values, autoLogin: this.state.autoLogin });
        FetchAPI(`/api/user/login`, 'POST', values).then((result) => {
            debugger;
            // let data = this.props.form.getFieldsValue()
            // let history = this.context.router.history;
            if (result.code === 'ACM0000') {
                // 将返回数据的json对象存入session
                setCurrentLoginUser(result.data);
                //跳转后台管理界面
                window.location.href = '/admin/UserList';
            } else {
                this.warning(result.message)
            }
        }, (err) => {
            this.error()
        });
    }
    onTabChange = (key) => {
        this.setState({
            type: key,
        });
    }
    changeAutoLogin = (e) => {
        this.setState({
            autoLogin: e.target.checked,
        });
    }
    render() {
        return (
            <Login
                onSubmit={this.onSubmit}
                className="login"
            >
                <div className="login-logo">
                    <img src={logo} alt="吉林建筑大学电气与计算机学院"/>
                </div>
                <div className="title-admin">
                    <h2 color={"#3883d1"}>电气与计算机学院门户管理后台</h2>
                </div>
                {
                    this.state.notice &&
                    <Alert style={{ marginBottom: 24 }} message={this.state.notice} type="error" showIcon closable />
                }
                <UserName name="acNo" />
                <Password name="password" />
                <div>
                    <Checkbox checked={this.state.autoLogin} onChange={this.changeAutoLogin}>记住我</Checkbox>
                    <a style={{ float: 'right' }} href="/forget">忘记密码</a>
                </div>
                <Submit>登陆</Submit>
            </Login>
        );
    }
}

ReactDOM.render(<LoginDemo />, document.getElementById('content'));